/*
 * @brief: 三角设置类，用于电机款机器三角设置
 *
 * ChangeLog
 * Date             Author          Notes
 * 2015.12.23       Herbert         First Version
 */
#include "camsetting.h"
//#include "window.h"
//#include "message.h"
#include <malloc.h>
#include "motor_cam.h"
#include <assert.h>
#include "feedback.h"
#include <string.h>
#include <unistd.h>
#include "pthread.h"
#include "globe.h"

CAM_SETTING *cam_setting_new(MH_UDP *pudp)
{
    CAM_SETTING *cam_setting = (CAM_SETTING*)malloc(sizeof(CAM_SETTING));
    if (cam_setting == NULL)
    {
        return NULL;
    }

    memset(cam_setting, 0, sizeof(CAM_SETTING));

    cam_setting->pudp = pudp;
    cam_setting->exec = motor_cam_exec;
    cam_setting->reset = motor_cam_reset;
    cam_setting->set_zero_delta = motor_cam_set_zero_delta;
    cam_setting->th_check = (pthread_t)NULL;

    return cam_setting;
}

void cam_setting_close(CAM_SETTING *cam_setting)
{
    assert(cam_setting != NULL);

    if (cam_setting->th_check != (pthread_t)NULL)
    {
        pthread_join(cam_setting->th_check, NULL);
    }

}

void cam_setting_set_update_window_func(CAM_SETTING *cam_setting, UPDATEWND_FUNC update_wnd)
{
    assert(cam_setting != NULL);
    cam_setting->update_wnd = update_wnd;
}

int cam_setting_set_zero_delta(CAM_SETTING *cam_setting, char *delta)
{
    assert(cam_setting != NULL);
    assert(delta != NULL);

    if (cam_setting->set_zero_delta == NULL)
    {
        return 0;
    }

    return cam_setting->set_zero_delta(cam_setting->pudp, delta);
}

/*
 * @note: 检测三角电机报警
 */
void *cam_setting_check(MH_UDP * pudp, void *user_data)
{
    CAM_SETTING *cam_setting = (CAM_SETTING*)user_data;
    FEEDBACK fb;
    int error[4] = { 0 };
    int i = 0;
    if (cam_setting == NULL)
    {
        pthread_exit(0);
    }

    for (i = 0; i < cam_setting->timeout; i++)
    {
        usleep(2000);
    }

    if (feedback_get_error_state(pudp, &fb, 0) < 0)
    {
        cam_setting->warn = -5;
        pthread_exit((void*)-5);
    }
#if (MH_SYS_CAM == 1)
    error[0] = fb.error.qxk;
    error[1] = fb.error.hxk;
    error[2] = 0;
    error[3] = 0;
#else
    #if (MH_JTCS_CAM == MAC_3G_CAM)
    error[0] = fb.error.q3;
    error[1] = fb.error.h3;
    error[2] = fb.error.q4;
    error[3] = fb.error.h4;
    #else
    error[0] = fb.error.fcam1;
    error[1] = fb.error.bcam1;
    error[2] = fb.error.fcam2;
    error[3] = fb.error.bcam2;
    #endif
#endif

    cam_setting->warn = 0;
    for (i = 0; i < 4; i++)
    {
        if (error[i])
        {
            cam_setting->warn = -i - 1;
            break;
        }
    }

    if (cam_setting->update_wnd != NULL)
    {
        cam_setting->update_wnd();
    }
    pthread_exit(0);
}

int cam_setting_reset(CAM_SETTING *cam_setting)
{
    assert(cam_setting != NULL);

    short find_zero[4] = { 0 };
    int ret = 0;

    find_zero[0] = TRUE;
    find_zero[1] = TRUE;
    find_zero[2] = TRUE;
    find_zero[3] = TRUE;

    if (cam_setting->reset == NULL)
    {
        return 0;
    }

    ret = cam_setting->reset(cam_setting->pudp, 0, find_zero);
    memset(cam_setting->pos, 0, sizeof(cam_setting->pos));

    if (ret == 0)
    {
        if (cam_setting->th_check != (pthread_t)NULL)
        {
            pthread_join(cam_setting->th_check, NULL);
            cam_setting->th_check = (pthread_t)NULL;
        }
        cam_setting->timeout = 40;
        pthread_create(&cam_setting->th_check, NULL, (void *)cam_setting_check, (void *)cam_setting);
    }

    return ret;
}

int cam_setting_exec(CAM_SETTING *cam_setting, int index, short pos)
{
    assert(cam_setting != NULL);

    short delta[4] = { 0 };
    short find_zero[4] = { 0 };
    int ret = 0;

    delta[index] = pos - cam_setting->pos[index];
    if (pos == 0)
    {
        find_zero[index] = TRUE;
    }

    cam_setting->pos[index] = pos;

    if (cam_setting->exec == NULL)
    {
        return 0;
    }

    ret = cam_setting->exec(cam_setting->pudp, 0, delta, find_zero);
    if (ret == 0)
    {
        if (cam_setting->th_check != (pthread_t)NULL)
        {
            pthread_join(cam_setting->th_check, NULL);
            cam_setting->th_check = (pthread_t)NULL;
        }
        cam_setting->timeout = 40;
        pthread_create(&cam_setting->th_check, NULL, (void *)cam_setting_check, (void *)cam_setting);
    }

    return ret;
}


int cam_setting_get_warn(CAM_SETTING *cam_setting)
{
    assert(cam_setting != NULL);

    return cam_setting->warn;
}










